#include <bits/stdc++.h>
using namespace std;
#define int long long



void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> a(n + 1, vector<int>(m + 1));
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            scanf("%lld", &a[i][j]);
            a[i][j] |= a[i - 1][j];
        }
    }
    while(q--) {
        int k;
        scanf("%lld", &k);
        map<int, int> mp1, mp2;
        for(int i = 0; i < k; i++) {
            int t1, t2;
            string op;
            scanf("%lld", &t1);
            cin >> op;
            scanf("%lld", &t2);
            if(op == ">") {
                mp1[t1] = max(mp1[t1], t2);
            } else{
                if(mp2.count(t1))
                    mp2[t1] = min(mp2[t1], t2);
                else mp2[t1] = t2;
            }
        }

        auto check1 = [&](int x) ->bool{
            for(auto [k, v] : mp1) {
                if(a[x][k] <= v) return false;
            }
            return true;
        };
        auto check2 = [&](int x)->bool{
            for(auto [k, v] : mp2) {
                if(a[x][k] >= v) return false;
            }
            return true;
        };

        int l = 1, r = n;
        while(l < r) {
            int mid = (l + r) >> 1;
            if(check1(mid)) r = mid;
            else l = mid + 1;
        }
        if(!check1(l)) {
            cout << -1 << endl;
            continue;
        }
        int t = l;
        l = 1, r = n;
        while(l < r) {
            int mid = (l + r + 1) >> 1;
            if(check2(mid)) l = mid;
            else r = mid - 1;
        }
        if(!check2(l) || t > l){
            cout << -1 << endl;
            continue;
        }
        cout << t << endl;
    }
}

signed main() {
    int t = 1;
    while(t--) solve();
    return 0;
}